package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import db.DBConnecter;

import entity.Customer;

import flex.messaging.io.ArrayList;

public class CustomerDAO {
	private DBConnecter db;
	public CustomerDAO() {
		db = new DBConnecter();//initialize a new connection
	}
	
	
	//return only one customer
	public Customer getOneCustomer(String userid){
		ResultSet rs;
		String sql = "select * from customer where cst_id = '"+userid+"'";
		Customer client = new Customer();
		try{
			rs = db.query(sql);
			rs.next();//default only one customer returns
			client.setCst_created_date(rs.getString("cst_created_date"));
			client.setCst_email(rs.getString("cst_email"));
			client.setCst_first_name(rs.getString("cst_first_name"));
			client.setCst_home_address(rs.getString("cst_home_address"));
			client.setCst_home_phone(rs.getString("cst_home_phone"));
			client.setCst_id(rs.getString("cst_id"));
			client.setCst_last_name(rs.getString("cst_last_name"));
			client.setCst_mobile_phone(rs.getString("cst_mobile_phone"));
			client.setCst_password(rs.getString("cst_password"));
			client.setCst_state(rs.getString("cst_state"));
			rs.close();
			db.close();
			return client;
			
		}catch(SQLException e){
			e.printStackTrace();
			db.close();
			return client;
		}
		
	}
	
	
	
	//query and return some customers, but the real query function should not like this 
	public List<Customer> queryCustomers(String userid, String userfn, String userln){
		ResultSet rs;
		List<Customer> clientList = new ArrayList();
		String sql = "select * from customer where cst_id = '"+userid+"' or cst_first_name = '"+userfn+"' or cst_last_name = '"+userln+"'";
		
		try{
			rs = db.query(sql);
			while(rs.next()){
				Customer client = new Customer();
				client.setCst_created_date(rs.getString("cst_created_date"));
				client.setCst_email(rs.getString("cst_email"));
				client.setCst_first_name(rs.getString("cst_first_name"));
				client.setCst_home_address(rs.getString("cst_home_address"));
				client.setCst_home_phone(rs.getString("cst_home_phone"));
				client.setCst_id(rs.getString("cst_id"));
				client.setCst_last_name(rs.getString("cst_last_name"));
				client.setCst_mobile_phone(rs.getString("cst_mobile_phone"));
				client.setCst_password(rs.getString("cst_password"));
				client.setCst_state(rs.getString("cst_state"));
				clientList.add(client);
			}
			
			return clientList;
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return clientList;
	}
	
}
